/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.CourseSigninConfig;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.CourseSigninConfigRepository;
import me.zhengjie.modules.system.service.CourseSigninConfigService;
import me.zhengjie.modules.system.service.dto.CourseSigninConfigDto;
import me.zhengjie.modules.system.service.dto.CourseSigninConfigQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.CourseSigninConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author 王宇
* @date 2024-03-14
**/
@Service
@RequiredArgsConstructor
public class CourseSigninConfigServiceImpl implements CourseSigninConfigService {

    private final CourseSigninConfigRepository courseSigninConfigRepository;
    private final CourseSigninConfigMapper courseSigninConfigMapper;

    @Override
    public Map<String,Object> queryAll(CourseSigninConfigQueryCriteria criteria, Pageable pageable){
        Page<CourseSigninConfig> page = courseSigninConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(courseSigninConfigMapper::toDto));
    }

    @Override
    public List<CourseSigninConfigDto> queryAll(CourseSigninConfigQueryCriteria criteria){
        return courseSigninConfigMapper.toDto(courseSigninConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CourseSigninConfigDto findById(Long signinConfigId) {
        CourseSigninConfig courseSigninConfig = courseSigninConfigRepository.findById(signinConfigId).orElseGet(CourseSigninConfig::new);
        ValidationUtil.isNull(courseSigninConfig.getSigninConfigId(),"CourseSigninConfig","signinConfigId",signinConfigId);
        return courseSigninConfigMapper.toDto(courseSigninConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CourseSigninConfigDto create(CourseSigninConfig resources) {
        return courseSigninConfigMapper.toDto(courseSigninConfigRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CourseSigninConfig resources) {
        CourseSigninConfig courseSigninConfig = courseSigninConfigRepository.findById(resources.getSigninConfigId()).orElseGet(CourseSigninConfig::new);
        ValidationUtil.isNull( courseSigninConfig.getSigninConfigId(),"CourseSigninConfig","id",resources.getSigninConfigId());
        courseSigninConfig.copy(resources);
        courseSigninConfigRepository.save(courseSigninConfig);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long signinConfigId : ids) {
            courseSigninConfigRepository.deleteById(signinConfigId);
        }
    }

    @Override
    public void download(List<CourseSigninConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CourseSigninConfigDto courseSigninConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("课程编号", courseSigninConfig.getCourseNo());
            map.put("班级编号", courseSigninConfig.getClassNo());
            map.put("签到开始时间", courseSigninConfig.getStartTime());
            map.put("签到结束时间", courseSigninConfig.getEndTime());
            map.put("创建时间", courseSigninConfig.getCreateTime());
            map.put("创建人", courseSigninConfig.getCreateby());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}